<?php
declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Logic\Login as LoginLogic;

class LoginController extends Controller
{
    private $loginLogic;

    public function __construct()
    {
        $this->loginLogic = new LoginLogic();
    }

    public function login(Request $request)
    {
        if ($request->isMethod('post')) {
            $result = ['status' => 0, 'message' => '登录失败！'];

            // 1.判断 csrf_token 是否验证通过，laravel csrf_token的验证在中间件里，这里不需要处理

            // 2.验证图形验证码
            if ($request->session()->get("account_login_num") > 0) {
                $validator = Validator::make($request->all(), ['vcode' => 'required|captcha']);
                if ($validator->fails()) {
                    $request->session()->put('account_login_num', 1);
                    return response()->json(['status' => 0, 'message' => "验证码错误！"]);
                }
            }

            // 3.同一个浏览器连续登录失败3次后，必须等待5分钟后才能登录
            if ($request->session()->get("account_login_error") > 3 && time() - $request->session()->get("account_login_time") <= 300 ){
                $result['message'] = "你已连续登录失败3次，请5分钟后再登录！";
                return response()->json($result);
            }

            $account = $request->input("account");
            $password = $request->input("password");
            $validator = Validator::make($request->all(), [
                'account' => 'required|alpha_dash|min:2',
                'password' => 'required|min:6|max:32',
            ]);
            if ($validator->fails()) {
                $request->session()->put('account_login_num', 1);
                return response()->json(['status' => 0, 'message' => "账号或密码输入有误！"]);
            }

            if ($this->loginLogic->login($account, $password)) {
                $request->session()->put("account_login_num", 0);
                $request->session()->put("account_login_time", time());
                $request->session()->put("account_login_error", 0);
                $result['status'] = 1;
                $result['message'] = "登录成功！";
                return response()->json($result);
            }else{
                if ($request->session()->get("account_login_error") > 0) {
                    $request->session()->put("account_login_error", $request->session()->get("account_login_error") + 1);
                } else {
                    $request->session()->put("account_login_error", 1);
                }
                if ($request->session()->get("account_login_num") > 0) {
                    $request->session()->put("account_login_num", $request->session()->get("account_login_num") + 1);
                } else {
                    $request->session()->put("account_login_num", 1);
                }
                $request->session()->put("account_login_time", time());
                $result['message'] = "账号或者密码错误！";
                return response()->json($result);
            }
        } else {
            return view('login.login');
        }
    }

    public function logout(Request $request)
    {
        $request->session()->forget("account");
        $request->session()->forget("account_id");
        $request->session()->forget("account_name");
        $request->session()->forget("account_login_name");
        $request->session()->forget("account_login_time");
        $request->session()->forget("account_login_error");
        $result = ['status' => 1, 'message' => '退出成功！'];
        return response()->json($result);
    }
}
